﻿using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Model
{
  /// <summary>
  /// 格子
  /// </summary>
  public class UISlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IDisposable
  {
    /// <summary>
    /// 预制体
    /// </summary>
    public GameObject ItemPrefab;

    /// <summary>
    /// 关联的UI项
    /// </summary>
    public UIItem UIItem { get; set; }

    /// <summary>
    /// 相关联的数据
    /// </summary>
    public ItemViewModel VM { get; set; }

    public bool isTips = true;
    public bool isDrag = true;

    public static Action<Transform, ItemEntity> OnEnter;
    public static Action<Transform> OnExit;

    public static Action<UISlot> OnLeftBeginDrag;
    public static Action<UISlot, GameObject> OnLeftEndDrag;
    public static Action<UISlot> OnLeftDrag;

    /// <summary>
    /// 存放物品
    /// </summary>
    /// <param name="item"></param>
    public void StoreItem(ItemViewModel item)
    {
      // 空格子
      if (UIItem == null)
      {
        GameObject go = GameObject.Instantiate<GameObject>(ItemPrefab, this.transform);
        this.UIItem = go.GetComponent<UIItem>();
        this.VM = item;
      }
      else
      {
        VM.Count++;
      }
      this.UIItem.Show(VM);
    }

    /// <summary>
    /// 鼠标退出
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
      if (OnExit != null && this.VM != null)
        OnExit(transform);
    }

    /// <summary>
    /// 鼠标进入
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
      if (OnEnter != null && this.VM != null)
        OnEnter(transform, this.VM.Item);
    }

    /// <summary>
    /// 鼠标按下
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerDown(PointerEventData eventData)
    {

    }

    /// <summary>
    /// 开始拖拽
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData)
    {
      if (!isDrag) return;

      if (eventData.button == PointerEventData.InputButton.Left)
      {
        if (OnLeftBeginDrag != null)
          OnLeftBeginDrag(this);
      }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
      if (!isDrag) return;

      if (eventData.button == PointerEventData.InputButton.Left)
      {
        if (OnLeftEndDrag != null)
        {
          if (eventData.pointerEnter == null) // 拖放结束时，如果物体是空的。
          {
            OnLeftEndDrag(this, null);
          }
          else
          {
            OnLeftEndDrag(this, eventData.pointerEnter);
          }
        }
      }
    }

    public void OnDrag(PointerEventData eventData)
    {
      if (!isDrag) return;

      if (eventData.button == PointerEventData.InputButton.Left)
      {
        if (OnLeftDrag != null)
          OnLeftDrag(this);
      }
    }

    /// <summary>
    /// 替换这个Slot信息
    /// </summary>
    /// <param name="vm"></param>
    /// <returns></returns>
    public ItemViewModel Replace(ItemViewModel vm)
    {
      var tempVm = this.VM;
      if (this.UIItem == null)
      {
        this.StoreItem(vm);
      }
      else
      {
        this.VM = vm;
        this.UIItem.Show(VM);
      }
      return tempVm;
    }

    public void Dispose()
    {
      Destroy(UIItem.gameObject);
      VM = null;
      UIItem = null;
    }

    /// <summary>
    /// 是否能方这种类型的物品
    /// 默认什么都能放
    /// </summary>
    public virtual bool Has(ItemViewModel vm)
    {
      return true;
    }

    /// <summary>
    /// 是否是null格子
    /// </summary>
    public bool IsEmpty
    {
      get
      {
        return this.UIItem == null;
      }
    }

    ///
    public UIInventory UIInventory
    {
      get
      {
        return transform.parent.parent.parent.GetComponent<UIInventory>();
      }
    }

  }
}